

package com.hazelcast.spi.impl.operationservice;

import com.hazelcast.logging.ILogger;

public final class OperationResponseHandlerFactory {

    private static final NoResponseHandler EMPTY_RESPONSE_HANDLER = new NoResponseHandler();

    private OperationResponseHandlerFactory() {
    }

    public static OperationResponseHandler createEmptyResponseHandler() {
        return EMPTY_RESPONSE_HANDLER;
    }

    public static OperationResponseHandler createErrorLoggingResponseHandler(ILogger logger) {
        return new ErrorLoggingResponseHandler(logger);
    }

    private static class NoResponseHandler implements OperationResponseHandler {

        @Override
        public void sendResponse(Operation op, Object obj) {
        }
    }

    private static final class ErrorLoggingResponseHandler implements OperationResponseHandler {
        private final ILogger logger;

        private ErrorLoggingResponseHandler(ILogger logger) {
            this.logger = logger;
        }

        @Override
        public void sendResponse(Operation op, Object obj) {
            if (obj instanceof Throwable throwable) {
                logger.severe(throwable);
            }
        }
    }
}
